const app = getApp();
const pageSize = 15;
const rechargeOptions = [{
  value: 1,
  name: "已启用"
}, {
  value: 2,
  name: "已禁用"
}];
const thrashOptions = [{
  value: 1,
  name: "已启用"
}, {
  value: 2,
  name: "已禁用"
}]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFilter: false,
    drawer: false,
    villageOptions: [],
    thrashOptions,
    rechargeOptions,
    conditions: {
      thrashIndex: "",
      village_name: "",
      rechargeIndex: "",
    },
    page: 1,
    addressList: [],
    loadmore: false,
    nomore: false,
    total: 0,
    total_card: 0,
    total_equipment: 0,
  },


  onLoad: function (options) {
    this.VillageList();
    // this.getAllVillage();
  },


  reloadData() {
    this.setData({
      page: 1,
      addressList: [],
      loadmore: false,
      nomore: false,
      total: 0,
      total_card: 0,
      total_equipment: 0,
    })
    wx.showLoading();
    this.VillageList();
  },



  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.VillageList();
    }
  },



  // 经营设置
  communitySetting(e) {
    wx.navigateTo({
      url: '../communitySetting/communitySetting?v_id=' + e.currentTarget.dataset.v_id,
    })
  },

  // 编辑园区
  editCommunity: function (e) {
    wx.navigateTo({
      url: '../editCommunity/editCommunity?type=1&v_id=' + e.currentTarget.dataset.v_id,
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  // 删除园区
  delCommunity: function (e) {
    var that = this;
    var v_id = e.currentTarget.dataset.v_id;
    const name = e.currentTarget.dataset.name;
    wx.showModal({
      title: '提示',
      content: '是否删除该园区',
      success: function (res) {
        if (res.confirm) {
          app.ajax({
            url: 'Partner/Village/deleteVillage',
            method: "POST",
            data: {
              id: v_id
            },
            success: function (res) {
              if (res.data.code > 0) {
                if (that.village_id !== "" && that.village_id == v_id) {
                  delete(that.village_id)
                  that.setData({
                    "conditions.village_name": ''
                  })
                }
                var flag = res.data;
                app.showToast(flag.message, "success");
                setTimeout(() => {
                  // that.getAllVillage();
                  that.reloadData();
                }, 1500)
              } else {
                app.showToast(res.data.message)
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },



  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },



  handleCondition(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },



  VillageList() {
    var that = this;
    app.ajax({
      url: 'Partner/Village/VillageList',
      method: "POST",
      data: {
        row: pageSize,
        page: that.data.page,
        village_name: that.data.conditions.village_name,
        template_status: that.data.conditions.rechargeIndex !== '' ? that.data.rechargeOptions[that.data.conditions.rechargeIndex].value : '',
        sell_water_status: that.data.conditions.thrashIndex !== '' ? that.data.thrashOptions[that.data.conditions.thrashIndex].value : ''
      },
      success: function (res) {
        wx.hideLoading();
        wx.stopPullDownRefresh()
        if (res.data.code > 0) {
          const source = res.data.data;
          let addressList = that.data.addressList.concat(source.data);
          that.setData({
            drawer: false,
            empty: source.page.page_count == 0 ? true : false,
            addressList: addressList,
            nomore: source.page.total_number <= addressList.length ? true : false,
            loadmore: source.page.total_number > addressList.length ? true : false,
            total: source.page.total_number,
            total_card: source.total_card_number,
            total_equipment: source.total_equipment_number
          })
        } else {
          app.showToast(res.data.message);
        }
      },
      error: e => {
        wx.hideLoading();
        wx.stopPullDownRefresh()
      }
    })
  },



  // 添加园区
  addCommunity: function () {
    wx.navigateTo({
      url: '../editCommunity/editCommunity?type=2',
    })
  },


  //跳转到选择园区
  navMiddleVillage() {
    let _self = this;
    wx.navigateTo({
      url: "../middle/village/index?hidden=1",
      events: {
        acceptDataFromMiddleVillage: function (data) {
          if (data) {
            _self.village_id = data.id;
            _self.setData({
              [`conditions.village_name`]: data.village_name
            })
          }
        }
      },
      success: res => {
        res.eventChannel.emit("acceptDataFromOpenerPage", {
          village_id: this.village_id || ''
        })
      }
    })
  },



  getAllVillage() {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/getPartnerVillage',
      success: function (res) {
        var flag = res.data;
        if (flag.code > 0) {
          var name = [];
          for (var i = 0; i < flag.data.length; i++) {
            name[i] = flag.data[i].village_name;
          }
          that.setData({
            villageOptions: name
          });
        }
      }
    })
  },
})